#include<iostream>
using namespace std;
typedef long long LL;
const int N = 1e3 + 10;
int a[N];
LL s[N];
int n;
//int main()
//{
//	cin >> n;
//	bool f = 0;
//	for (int i = 1; i <= n; i++) {
//		cin >> a[i];
//		s[i] = s[i - 1] + a[i];
//		if (a[i] == a[i - 1])f = 1;
//	}
//	if (f) {
//		cout << 0 << endl;
//		return 0;
//	}
//	LL ans = 1e15;
//	for (int i = 1; i <= n - 1; i++) {
//		for (int j = i; j <= n - 1; j++) {
//			for (int k = j + 1; k <= n; k++) {
//				for (int z = k; z <= n; z++) {
//					LL x = s[j] - s[i - 1];
//					LL y = s[z] - s[k - 1];
//					ans = min(ans, abs(x - y));
//				}
//			}
//		}
//	}
//	cout << ans << endl;
//	return 0;
//}
int main()
{
	cin >> n;
	bool f = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		s[i] = s[i - 1] + a[i];
		if (a[i] == a[i - 1])f = 1;
	}
	if (n == 1) {
		cout << a[1] << endl;
		return 0;
	}
	if (f) {
		cout << 0 << endl;
		return 0;
	}
	/*for (int i = 1; i <= n; i++) {
		cout << s[i] << " ";
	}
	cout << endl;*/
	if (n <= 3) {
		if (n == 2) {
			cout << abs(a[1] - a[2]) << endl;
			return 0;
		}
		else {
			LL a1 = abs(a[1] - a[2]);
			LL a2 = abs((a[1] + a[2]) - a[3]);
			LL a3 = abs(a[2] - a[3]);
			LL a4 = abs(a[1] - (a[2] + a[3]));
			cout << min(min(a1, a2), min(a3, a4)) << endl;
			return 0;
		}
	}
	LL ans = 1e15;
	for (int i = 1; i <= n - 1; i++) {
		for (int j = i; j <= n - 1; j++) {
			for (int k = j + 1; k <= n; k++) {
				int l = k - 1;
				int r = n + 1;
				LL x = s[j] - s[i - 1];
				//cout << "x==" << x << endl;
				while (l + 1 != r) {
					int mid = l + r >> 1;
					if (s[mid] - s[k - 1] >= x)r--;
					else l++;
				}
				int z1 = 0, z2 = 0;
				z1 = r;
				l = k - 1, r = n + 1;
				while (l + 1 != r) {
					int mid = l + r >> 1;
					if (s[mid] - s[k - 1] <= x)l++;
					else r--;
				}
				z2 = l;
				/*cout << "i==" << i << endl;
				cout << "j==" << j << endl;
				cout << "k==" << k << endl;
				cout << "z1==" << z1 << endl;
				cout << "z2==" << z2 << endl;*/
				LL aa = s[z1] - s[k - 1];
				LL bb = s[z2] - s[k - 1];
				ans = min(ans, min(abs(aa - x), abs(bb - x)));
			}
		}
	}
	cout << ans << endl;
	return 0;
}